/**
 * 
 */
package oj.swing.component;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.zip.DataFormatException;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import oj.jboss.seContext.SContext;
import oj.swing.common.DataSSearch;
import oj.swing.common.DataSSearchField;
import oj.swing.common.DataUtils;
import oj.swing.interfaces.OJSwingTableHome;
import oj.swing.interfaces.OJSwingTableRemote;

import com.intelap.dbswing.JdbNavToolBar;
import com.intelap.dx.dataset.Column;
import com.intelap.dx.dataset.DataChangeEvent;
import com.intelap.dx.dataset.DataChangeListener;
import com.intelap.dx.dataset.DataSet;
import com.intelap.dx.dataset.EditAdapter;
import com.intelap.dx.dataset.NavigationEvent;
import com.intelap.dx.dataset.NavigationListener;
import com.intelap.dx.dataset.ReadRow;
import com.intelap.dx.dataset.ReadWriteRow;
import com.intelap.dx.dataset.StatusEvent;
import com.intelap.dx.dataset.StatusListener;
import com.intelap.dx.dataset.StorageDataSet;
import com.intelap.dx.dataset.Variant;
import com.intelap.iaswing.IASwingNavToolBar;
import com.intelap.iaswing.IASwingScrollPane;
import com.intelap.iaswing.IASwingTable;

/**
 * @author Omar
 * 
 */
public class OJSwingSearchTable {

	private SContext context = null;

	private OJSwingTableHome home;

	private OJSwingTableRemote remote;

	private HashMap fields;

	private String id_search;

	private IASwingTable table;

	protected StorageDataSet dataset;

	private DataSSearch search;

	public OJSwingSearchTable(String idSearch) throws Exception {

		context = new SContext();
		this.id_search = idSearch;
		initGUI();
	}

	private void initGUI() throws Exception {

		try {
			home = (OJSwingTableHome) context.getHome("OJSwingTable",
					OJSwingTableHome.class);
			remote = home.create();

			search = (DataSSearch) remote.getDataSearch(id_search);
			fields = (HashMap) remote.getFieldSearch(id_search);

		} catch (Exception e) {
			throw e;
		}

		dataset = createOjDataSet();
		addListenerDataset();

		table = new IASwingTable();		
		table.setDataSet(dataset);

	}

	// private void insertRow() {
	//
	// dataset.insertRow(false);
	// dataset.getColumn(COL_IDATTACHMENT).setReadOnly(false);
	//
	// /**
	// * Crea el archivo - OJG - 28/01/2009 - Modificacion para elejir el path
	// * de destino
	// */
	// dataset.setString(COL_IDATTACHMENT, "132465465");
	//
	// dataset.getColumn(COL_IDATTACHMENT).setReadOnly(true);
	// dataset.getColumn(COL_DESCRIPTION).setReadOnly(false);
	// dataset.setString(COL_DESCRIPTION, "OMAR ES GROSO ");
	//
	// dataset.getColumn(COL_TITLE).setReadOnly(false);
	// dataset.setString(COL_TITLE, "PRUEBA DATASET");
	// /** OJG - Seteo el titulo en ReadOnly - 28/01/2009 */
	// dataset.getColumn(COL_TITLE).setReadOnly(true);
	//
	// dataset.getColumn(COL_FILENAME).setReadOnly(false);
	// dataset.setString(COL_FILENAME, "VAMOS A VER");
	// dataset.getColumn(COL_FILENAME).setReadOnly(true);
	// dataset.post();
	//
	// }

	// protected JPanel createGrillaAttachment() {
	// JPanel panel = new JPanel(new BorderLayout());
	// IASwingTable table = new IASwingTable();
	// IASwingScrollPane scrollPane = new IASwingScrollPane();
	// scrollPane = new IASwingScrollPane();
	// scrollPane.getViewport().add(table);
	// scrollPane.setEnabled(true);
	// table.setVisible(true);
	//
	// /** Crea el Dataset - OJG */
	// dataset = createDataSet();
	// System.out.println("Dataset");
	// addListenerDataset();
	//
	// table.setDataSet(dataset);
	// JPanel navPanel = new JPanel(new BorderLayout());
	// navPanel.setBorder(BorderFactory.createEtchedBorder());
	// navPanel.add(createNavToolBar(), BorderLayout.CENTER);
	// panel.add(scrollPane, BorderLayout.CENTER);
	// panel.add(navPanel, BorderLayout.SOUTH);
	// panel.setBounds(0, 0, 600, 500);
	// panel.setVisible(true);
	// return panel;
	// }

	// static final String COL_DESCRIPTION = "DESCRIPTION";
	//
	// static final String COL_TITLE = "TITLE";
	//
	// static final String COL_IDATTACHMENT = "ID_ATTACHMENT";
	//
	// static final String COL_FILENAME = "FILENAME";
	//
	// static final String DESCRIPTION_COL_DESCRIPTION =
	// "DESCRIPTION_COL_DESCRIPTION";
	//
	// static final String DESCRIPTION_COL_TITLE = "DESCRIPTION_COL_TITLE";
	//
	// static final String DESCRIPTION_COL_FILENAME =
	// "DESCRIPTION_COL_FILENAME";
	//
	// private StorageDataSet createDataSet() {
	// StorageDataSet ds = new StorageDataSet();
	// ds.setEnableUpdate(true);
	//
	// Column col = new Column(COL_IDATTACHMENT, COL_IDATTACHMENT,
	// Variant.STRING);
	// col.setRequired(true);
	// col.setVisible(0);
	// ds.addColumn(col);
	//
	// col = new Column(COL_TITLE, DESCRIPTION_COL_TITLE, Variant.STRING);
	// col.setRequired(false);
	// col.setReadOnly(false);
	// col.setEditable(true);
	//
	// ds.addColumn(col);
	// col = new Column(COL_DESCRIPTION, DESCRIPTION_COL_DESCRIPTION,
	// Variant.STRING);
	// col.setRequired(false);
	// col.setReadOnly(false);
	//
	// ds.addColumn(col);
	// col = new Column(COL_FILENAME, DESCRIPTION_COL_FILENAME, Variant.STRING);
	// col.setRequired(true);
	//
	// ds.addColumn(col);
	// ds.restructure();
	// ds.open();
	// return ds;
	// }

	public StorageDataSet createOjDataSet() throws Exception,
			DataFormatException {

		StorageDataSet ds = new StorageDataSet();

		if (fields != null && !fields.isEmpty()) {

			for (Iterator iter = fields.keySet().iterator(); iter.hasNext();) {

				String key = (String) iter.next();

				DataSSearchField dataSSField = (DataSSearchField) fields
						.get(key);

				Column col = new Column(key, dataSSField.getDESCRIPTION(),
						DataUtils.getVariant(dataSSField.getDATA_TYPE()));
				

				col.setRequired(true);
				col.setEditable(true);
				// col.setVisible(0);
				ds.addColumn(col);

			}
			ds.restructure();
			ds.open();

		}

		return ds;

	}

	private IASwingNavToolBar createNavToolBar() {
		// NavToolBar
		IASwingNavToolBar nav = new IASwingNavToolBar();
		nav.getSaveButton().removeActionListener(
				nav.getSaveButton().getActionListeners()[0]);
		nav.setFloatable(false);
		nav.setShowRollover(false);
		nav.setShowTooltips(false);
		nav.setDataSet(dataset);

		nav.setButtonStateCancel(JdbNavToolBar.HIDDEN);
		nav.setButtonStateDitto(JdbNavToolBar.HIDDEN);
		nav.setButtonStatePost(JdbNavToolBar.HIDDEN);
		nav.setButtonStateRefresh(JdbNavToolBar.HIDDEN);
		nav.setButtonStateCancel(JdbNavToolBar.HIDDEN);
		nav.setButtonStateSave(JdbNavToolBar.HIDDEN);
		nav.setButtonStateInsert(JdbNavToolBar.HIDDEN);
		nav.getDeleteButton().removeActionListener(
				nav.getDeleteButton().getActionListeners()[0]);
		nav.getDeleteButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (JOptionPane.showConfirmDialog(new JFrame(), "omar") == JOptionPane.YES_OPTION) {
					// String idAttachment =
					// dataset.getString(COL_IDATTACHMENT);
					dataset.deleteRow();
				}
			}
		});
		return nav;
	}

	private void addListenerDataset() {
		dataset.addEditListener(new EditAdapter() {
			public void updating(DataSet dataSet, ReadWriteRow newRow,
					ReadRow oldRow) throws Exception {
				System.out.println("actualizar");
				// update(dataset.getString(COL_IDATTACHMENT), dataset
				// .getString(COL_TITLE), dataset
				// .getString(COL_DESCRIPTION));
			}

			public void adding(DataSet dataSet, ReadWriteRow newRow)
					throws Exception {
				System.out.println("adding");
			}

			public void deleting(DataSet dataSet) throws Exception {
				System.out.println("deleting");

			}

			public void deleted(DataSet dataset) {
				System.out.println("deleted");
			}

		});
		dataset.addStatusListener(new StatusListener() {
			public void statusMessage(StatusEvent event) {

			}

		});
		dataset.addDataChangeListener(new DataChangeListener() {
			public void dataChanged(DataChangeEvent event) {

			}

			public void postRow(DataChangeEvent event) throws Exception {

			}
		});
		dataset.addNavigationListener(new NavigationListener() {
			public void navigated(NavigationEvent event) {
				// loadRemoteDocument();
			}
		});
	}

	public void beginTable() {

	}

	public IASwingTable getTable() {
		return table;
	}

	public void setTable(IASwingTable table) {
		this.table = table;
	}

	public DataSSearch getSearch() {
		return search;
	}

	public void setSearch(DataSSearch search) {
		this.search = search;
	}
}
